package com.core;
import java.io.Serializable;
import java.util.ArrayList;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Named;
/**
 * 
 * @author Martin S
 *
 */

@Named
@ApplicationScoped
public class Service implements Serializable{

	private ArrayList<User> users;
	
	public Service(){
		users = new ArrayList<User>();
		createSomeUsers();
		
	}

	public ArrayList<User> getUsers() {
		return users;
	}
	
	public void addUser(User user){
		users.add(user);
	}
	
	public void removeUser(User user){
		if (users.contains(user)){
			users.remove(user);
		}
	}
	
	public boolean userVerified(User user){
		boolean verified = false;
		int i = 0;
		while (!verified && i<users.size()){
			User temp = users.get(i);
			if (temp.getUserName().equals(user.getUserName()) &&
					temp.getPassword().equals(user.getPassword())){
				verified = true;
			}
			i++;
		}
		return verified;
	}
	
	public void updateUser(User user){
		for (User u : users){
			if (u.equals(user)){
				u.update(user);
				break;
			}
		}
	}
	
	public User getStoreduser(User user){
		User u = new User();
		boolean found = false;
		int i = 0;
		while (!found && i<users.size()){
			if (users.get(i).getUserName().equals(user.getUserName())){
				found = true;
				u = users.get(i);
			}
			i++;
		}
		return u;
	}
	
	public User getStoreduserOnId(User user){
		User u = new User();
		boolean found = false;
		int i = 0;
		while (!found && i<users.size()){
			if (users.get(i).getUserID() == user.getUserID()){
				found = true;
				u = users.get(i);
			}
			i++;
		}
		return u;
	}
	
	public void createSomeUsers(){
		addUser(new User("Administrator", "admin", "toor", users.size()));
		addUser(new User("Martin Roenn", "martin", "12345", users.size()));
		addUser(new User("Daniel Soerensen", "daniel", "23456", users.size()));
		addUser(new User("Martin Stendof", "martin_s", "34567", users.size()));
	}
}
